package com.czk.gulimall.ware.service.impl;

import com.alibaba.fastjson.TypeReference;
import com.czk.common.utils.R;
import com.czk.gulimall.ware.feign.MemberFeignService;
import com.czk.gulimall.ware.vo.FareVo;
import com.czk.gulimall.ware.vo.MemberAddressVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.czk.common.utils.PageUtils;
import com.czk.common.utils.Query;

import com.czk.gulimall.ware.dao.WareInfoDao;
import com.czk.gulimall.ware.entity.WareInfoEntity;
import com.czk.gulimall.ware.service.WareInfoService;
import org.springframework.util.StringUtils;


@Service("wareInfoService")
public class WareInfoServiceImpl extends ServiceImpl<WareInfoDao, WareInfoEntity> implements WareInfoService {
    @Autowired
    private MemberFeignService memberFeignService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String key =(String) params.get("key");

        QueryWrapper<WareInfoEntity> wrapper=new QueryWrapper<>();
        if (!StringUtils.isEmpty(key)){
            wrapper.and((wq)->{
                wq.eq("id",key)
                        .or()
                        .like("name",key)
                        .or()
                        .like("address",key)
                        .or()
                        .like("areacode",key);
            });
        }
        IPage<WareInfoEntity> page = this.page(
                new Query<WareInfoEntity>().getPage(params),
                wrapper
        );

        return new PageUtils(page);
    }

    @Override
    public FareVo getFare(Long addrId) {
        FareVo fareVo=new FareVo();
        R r = memberFeignService.info(addrId);
        MemberAddressVo memberAddressVo = (MemberAddressVo) r.getData("memberReceiveAddress",new TypeReference<MemberAddressVo>() {
        });
        if (memberAddressVo!=null) {
            String phone = memberAddressVo.getPhone();
            String substring = phone.substring(phone.length() - 1);
            fareVo.setAddress(memberAddressVo);
            fareVo.setFare(new BigDecimal(substring));
            return fareVo;
        }
        return null;
    }
}